<?php
namespace  app\project\controller;
use app\admin\model\Admin_model;
use think\Controller;
use think\Db;
use think\Request;

Class Miscellaneous extends Controller{
    function __construct(Request $request = null)
    {
        parent::__construct($request);
        if(session('user')) {
            $user=session('user');
            $admin_model=new Admin_model();
            $fun=$admin_model->get_rolefunction($user['role']);
            $this->assign('fun',$fun);
            $this->assign('active','杂项管理');
        }else{
            $this->redirect('index/login/index');
        }
    }
    //行业管理
    public function  industry(){
        $this->assign('title','行业管理');
        return $this->fetch();
    }
    public function  industry_list(){
        $where=array();
        if(isset($_GET['search'])&&$_GET['search']){
            $where['a.name|b.name']=['like','%'.$_GET['search'].'%'];
        }
        $industry=Db::name('industry')
            ->field('a.*,b.name as fu_name')->alias('a')
            ->join('industry b',' a.superior_id =b.id','left')
            ->where($where)
            ->select();
        foreach ($industry as &$value){
                $value['operation']='<a onclick="del_ind('.$value['id'].')" type="button" class="btn btn-sm btn-danger" >删除</a>';
        }
        return array('rows'=>$industry,'total'=>count($industry));
    }
    public function  add_industry(){
        if(request()->isPost()){
            $data=input('post.');
            if ($data['superior_id']){
                $fu=Db::name('industry')->where('id',$data['superior_id'])->find();
                $data['level']=$fu['level']+1;
            }else{
                $data['level']=1;
            }
            $id=Db::name('industry')->insertGetId($data);
            if($id){
                return array('code' => 1, 'msg' => '添加成功', 'url' => url('industry'));
            }else{
                return array('code' => 0, 'msg' => '添加失败',);
            }
        }
        $industry=Db::name('industry')->select();
        $this->assign('industry',$industry);
        $this->assign('title','添加行业');
        return $this->fetch();
    }
    public function  del_ind(){
        $ins_id=$_GET['id'];
        $info= Db::name('industry')->where('id',$ins_id)->delete();
        if($info){
            return array('code' => 1, 'msg' => '删除成功');
        }else{
            return array('code' => 0, 'msg' => '删除失败');
        }
    }
    //手机品牌管理
    public  function  phone_brand(){
        if(request()->isPost()){
            $data=input('post.');
            $id=Db::name('phone_brand')->insertGetId($data);
            if($id){
                return array('code' => 1, 'msg' => '添加成功', 'url' => url('phone_brand'));
            }else{
                return array('code' => 0, 'msg' => '添加失败',);
            }
        }
        $this->assign('title','手机品牌管理');
        return $this->fetch();
    }
    public function  phone_list(){
        $where=array();
        if(isset($_GET['search'])&&$_GET['search']){
            $where['name']=['like','%'.$_GET['search'].'%'];
        }
        $phpone_brand=Db::name('phone_brand')->select();
        foreach ($phpone_brand as &$value){
            $value['operation']='<a onclick="del_pho('.$value['id'].')" type="button" class="btn btn-sm btn-danger" >删除</a>';
        }
        return array('rows'=>$phpone_brand,'total'=>count($phpone_brand));
    }
    public function  del_pho(){
        $ins_id=$_GET['id'];
        $info= Db::name('phone_brand')->where('id',$ins_id)->delete();
        if($info){
            return array('code' => 1, 'msg' => '删除成功');
        }else{
            return array('code' => 0, 'msg' => '删除失败');
        }
    }
    //兴趣定向管理
    public  function  interest(){
        if(request()->isPost()){
            $data=input('post.');
            $id=Db::name('interest')->insertGetId($data);
            if($id){
                return array('code' => 1, 'msg' => '添加成功', 'url' => url('interest'));
            }else{
                return array('code' => 0, 'msg' => '添加失败',);
            }
        }
        $this->assign('title','兴趣定向管理');
        return $this->fetch();
    }
    public function  interest_list(){
        $where=array();
        if(isset($_GET['search'])&&$_GET['search']){
            $where['name']=['like','%'.$_GET['search'].'%'];
        }
        $interest=Db::name('interest')->select();
        foreach ($interest as &$value){
            $value['operation']='<a onclick="del_int('.$value['id'].')" type="button" class="btn btn-sm btn-danger" >删除</a>';
        }
        return array('rows'=>$interest,'total'=>count($interest));
    }
    public function  del_int(){
        $ins_id=$_GET['id'];
        $info= Db::name('interest')->where('id',$ins_id)->delete();
        if($info){
            return array('code' => 1, 'msg' => '删除成功');
        }else{
            return array('code' => 0, 'msg' => '删除失败');
        }
    }
    //app自定义定向管理
    public  function app_custom(){
        if(request()->isPost()){
            $data=input('post.');
            $id=Db::name('app_custom')->insertGetId($data);
            if($id){
                return array('code' => 1, 'msg' => '添加成功', 'url' => url('app_custom'));
            }else{
                return array('code' => 0, 'msg' => '添加失败',);
            }
        }
        $this->assign('title','app自定义定向管理管理');
        return $this->fetch();
    }
    public function  app_custom_list(){
        $where=array();
        if(isset($_GET['search'])&&$_GET['search']){
            $where['name']=['like','%'.$_GET['search'].'%'];
        }
        $app_custom=Db::name('app_custom')->select();
        foreach ($app_custom as &$value){
            $value['operation']='<a onclick="del_int('.$value['id'].')" type="button" class="btn btn-sm btn-danger" >删除</a>';
        }
        return array('rows'=>$app_custom,'total'=>count($app_custom));
    }
    public function  del_appcus(){
        $ins_id=$_GET['id'];
        $info= Db::name('app_custom')->where('id',$ins_id)->delete();
        if($info){
            return array('code' => 1, 'msg' => '删除成功');
        }else{
            return array('code' => 0, 'msg' => '删除失败');
        }
    }
    //渠道管理
    public  function channel(){
        if(request()->isPost()){
            $data=input('post.');
            $id=Db::name('channel')->insertGetId($data);
            if($id){
                return array('code' => 1, 'msg' => '添加成功', 'url' => url('channel'));
            }else{
                return array('code' => 0, 'msg' => '添加失败',);
            }
        }
        $this->assign('title','渠道管理');
        return $this->fetch();
    }
    public function  channel_list(){
        $where=array();
        if(isset($_GET['search'])&&$_GET['search']){
            $where['name']=['like','%'.$_GET['search'].'%'];
        }
        $app_custom=Db::name('channel')->select();
        foreach ($app_custom as &$value){
            $value['operation']='<a onclick="del_cha('.$value['id'].')" type="button" class="btn btn-sm btn-danger" >删除</a>';
        }
        return array('rows'=>$app_custom,'total'=>count($app_custom));
    }
    public function  del_channel(){
        $ins_id=$_GET['id'];
        $info= Db::name('channel')->where('id',$ins_id)->delete();
        if($info){
            return array('code' => 1, 'msg' => '删除成功');
        }else{
            return array('code' => 0, 'msg' => '删除失败');
        }
    }
    //app管理
    public  function app(){
        if(request()->isPost()){
            $data=input('post.');
            $id=Db::name('app')->insertGetId($data);
            if($id){
                return array('code' => 1, 'msg' => '添加成功', 'url' => url('app'));
            }else{
                return array('code' => 0, 'msg' => '添加失败',);
            }
        }
        $this->assign('title','app管理');
        return $this->fetch();
    }
    public function  app_list(){
        $where=array();
        if(isset($_GET['search'])&&$_GET['search']){
            $where['name']=['like','%'.$_GET['search'].'%'];
        }
        $app_custom=Db::name('app')->select();
        foreach ($app_custom as &$value){
            $value['operation']='<a onclick="del_app('.$value['id'].')" type="button" class="btn btn-sm btn-danger" >删除</a>';
        }
        return array('rows'=>$app_custom,'total'=>count($app_custom));
    }
    public function  del_app(){
        $ins_id=$_GET['id'];
        $info= Db::name('app')->where('id',$ins_id)->delete();
        if($info){
            return array('code' => 1, 'msg' => '删除成功');
        }else{
            return array('code' => 0, 'msg' => '删除失败');
        }
    }
    //应用偏好管理
    public  function apply_bias(){
        if(request()->isPost()){
            $data=input('post.');
            $id=Db::name('apply_bias')->insertGetId($data);
            if($id){
                return array('code' => 1, 'msg' => '添加成功', 'url' => url(''));
            }else{
                return array('code' => 0, 'msg' => '添加失败',);
            }
        }
        $this->assign('title','应用偏好管理');
        return $this->fetch();
    }
    public function  apply_bias_list(){
        $where=array();
        if(isset($_GET['search'])&&$_GET['search']){
            $where['name']=['like','%'.$_GET['search'].'%'];
        }
        $app_custom=Db::name('apply_bias')->select();
        foreach ($app_custom as &$value){
            $value['operation']='<a onclick="del_app('.$value['id'].')" type="button" class="btn btn-sm btn-danger" >删除</a>';
        }
        return array('rows'=>$app_custom,'total'=>count($app_custom));
    }
    public function  del_apply_bias(){
        $ins_id=$_GET['id'];
        $info= Db::name('apply_bias')->where('id',$ins_id)->delete();
        if($info){
            return array('code' => 1, 'msg' => '删除成功');
        }else{
            return array('code' => 0, 'msg' => '删除失败');
        }
    }
    //广告点击偏好管理
    public  function click_bias(){
        if(request()->isPost()){
            $data=input('post.');
            $id=Db::name('click_bias')->insertGetId($data);
            if($id){
                return array('code' => 1, 'msg' => '添加成功', 'url' => url(''));
            }else{
                return array('code' => 0, 'msg' => '添加失败',);
            }
        }
        $this->assign('title','点击偏好管理');
        return $this->fetch();
    }
    public function  click_bias_list(){
        $where=array();
        if(isset($_GET['search'])&&$_GET['search']){
            $where['name']=['like','%'.$_GET['search'].'%'];
        }
        $app_custom=Db::name('click_bias')->select();
        foreach ($app_custom as &$value){
            $value['operation']='<a onclick="del_app('.$value['id'].')" type="button" class="btn btn-sm btn-danger" >删除</a>';
        }
        return array('rows'=>$app_custom,'total'=>count($app_custom));
    }
    public function  del_click_bias(){
        $ins_id=$_GET['id'];
        $info= Db::name('click_bias')->where('id',$ins_id)->delete();
        if($info){
            return array('code' => 1, 'msg' => '删除成功');
        }else{
            return array('code' => 0, 'msg' => '删除失败');
        }
    }
}